package com.bluefox.uac.dto;

import com.bluefox.common.web.dto.AbstractDTO;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDate;
import java.util.Collection;
import java.util.List;

/**
 * @author SunFeng
 */


@Setter
@Getter
@ToString
public class UserDTO extends AbstractDTO implements UserDetails {

    /**
     * 用户的登录名称
     */


    private String username;
    /**
     * 用户的中文名称
     */


    private String fullName;
    /**
     * 用户的密码
     */

    private String password;

    /**
     * 用户的权限
     */


    private String roles;
    /**
     * 是否启用
     */

    private Integer enable;

    /**
     * 手机号码
     */

    private String phoneNumber;
    /**
     * 生日
     */

    private LocalDate birthday;
    /**
     * 地址
     */

    private String address;

    /**
     * 用户类型   用来区分医院用户与外部用户
     */

    private String type;
    /**
     * 描述
     */

    private String description;
    /**
     * 所属部门id
     */
    private Long deptId;
    /**
     * 所属部门名称
     */
    private String deptName;
    /**
     * 非数据库表中字段
     */


    private List<GrantedAuthority> authorities;

    public UserDTO(String username, String fullName, String password, String roles, Integer enable, String phoneNumber, LocalDate birthday, String address, String type, String description) {
        this.username = username;
        this.fullName = fullName;
        this.password = password;
        this.roles = roles;
        this.enable = enable;
        this.phoneNumber = phoneNumber;
        this.birthday = birthday;
        this.address = address;
        this.type = type;
        this.description = description;
    }

    public void setAuthorities(List<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public UserDTO() {
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }


    /**
     * 账户是否未过期,过期无法验证
     *
     * @return boolean
     */

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     *
     * @return boolean
     */

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     *
     * @return boolean
     */

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     *
     * @return boolean
     */
    @Override
    public boolean isEnabled() {
        return this.enable == 1;
    }

    @Override
    public boolean equals(Object obj) {
        boolean b = obj instanceof UserDTO ? this.username.equals(((UserDTO) obj).username) : false;
        return b;
    }

    @Override
    public int hashCode() {
        return this.username.hashCode();
    }
}
